#include "EventHandler.h"
#include "GameWorld.h"
#include "GameState.h"
#include "GUIManager.h"
#include "ExecutionLog.h"
#include "GameBoard.h"

#include "EntityTypes.h"
#include "Message Dispatcher.h"
#include "MessageStructs.h"

void EventHandler::HandleEvents(SDL_Event &event)
{
	World->m_Chatbox.HandleEvents(event, World->m_Keystates);

	if(World->m_OverlayMenu.GetActive())
		World->m_SoundGUI.HandleEvents(event);

	if(World->m_OverlayMenu.GetActive())
	{
		World->m_OverlayMenu.HandleEvents(event);

		// The rest of this code should not be run in an active overlay
		return;
	}

	switch(event.type)
	{

	// Mouse Related Events
	case SDL_MOUSEMOTION: 
		// Update the mouse
		World->UpdateMousePosition(event);
		break;

	case SDL_MOUSEBUTTONDOWN:
		HandleMouseButtons(event);
		break;

	case SDL_MOUSEBUTTONUP:
		HandleMouseButtons(event);
		break;

	// Keyboard Evetns
	case SDL_KEYDOWN:
		HandleKeyboardButtons(event);
		break;

	// Quit
	case SDL_QUIT:
		set_next_state(STATE_EXIT);
		break;
	}

}

void EventHandler::HandleMouseButtons(SDL_Event& event)
{
	switch(event.type)
	{
	// Mouse Button Down
	case SDL_MOUSEBUTTONDOWN:
	
		// Left click
		if(event.button.button == SDL_BUTTON_LEFT)
		{
			// Set Selection box position
			

			World->m_SelectionBox.x = World->m_GameMousePos.x;
			World->m_SelectionBox.y = World->m_GameMousePos.y;

			// If the click is in bounds of the Minimap, then the minimap a click should cause a jump
			// over there
			if(World->m_pGameMap->Minimap().inBounds(World->m_ScreenMousePos.x, World->m_ScreenMousePos.y))
			{
				World->GetMap()->Minimap().HandleScrolling(World->ScreenMousePos());
			}
		}
		break;

	// Mouse Button UP
	case SDL_MOUSEBUTTONUP:
		switch(event.button.button)
		{
		// Right Click
		case SDL_BUTTON_RIGHT:

			// if we are not in the minimap, move units based on where we clicked on the game screen
			if(!(World->GetMap()->Minimap().inBounds(World->ScreenMousePos().x, World->ScreenMousePos().y)))
				Dispatcher->DispatchMessage(SENDER_IRRELEVANT, World->m_pSctEnt->ID(), MoveToLocation, &Vector2D(event.button.x + ClipPlane->GetX(), event.button.y + ClipPlane->GetY()));
//			else // otherwise send them to where they clicked on the minimap
//				World->m_Manager.m_pCurrGroup->Move(World->GetMap()->Minimap().GetWorldPosFromMousePos(World->ScreenMousePos()));

			break;
			

		// Left Click
		case SDL_BUTTON_LEFT: 
			World->m_SelectionBox = InvalidRect;

			if(!GUI->GetPanelBounds(World->m_ScreenMousePos.x, World->m_ScreenMousePos.y) && !World->m_pGameMap->Minimap().inBounds(World->m_ScreenMousePos.x, World->m_ScreenMousePos.y))
			{
				GUI->RegisterPanel(Key->GetPanelIDs()->Game_Unit);
				
				// Remove character buttons from the panel and deactivate it 
				GUI->ClearButtons();
			}
			break;
			
		}
		break;
	}
}

void EventHandler::HandleKeyboardButtons(SDL_Event& event)
{

	switch(event.key.keysym.sym)
	{
		// Left CTRL
		// =========
		case SDLK_LCTRL:
		{
			EntityList::iterator gameEntity = World->m_Entities.begin();

			while(gameEntity != World->m_Entities.end())
			{
				if((*gameEntity)->IsBoundCircleOn())
					(*gameEntity)->boundCircleOff();
				else
					(*gameEntity)->boundCircleOn();

				++gameEntity;
				
			}
			break;
		}

		case SDLK_RCTRL:
		{
			World->GetNavGraph()->ShowEdges = !World->GetNavGraph()->ShowEdges;
			break;
		}

		case SDLK_RSHIFT:
		{
			World->m_bRenderCells = !World->m_bRenderCells;
			break;
		}

	// Alphabetical keys

		// 'K' Key
		// =======
		case SDLK_k:
		{
			MsgConstructBuilding message_info = {HutID, World->m_GameMousePos};
			Dispatcher->DispatchMessage(SENDER_IRRELEVANT, World->m_pSctEnt->ID(), BuildBuilding, &message_info);
			break;
		}


	}
}